﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CPETwit.Data.Entity;

namespace CPETwit.Data.Manager
{
    public class TweetManager
    {
        public void CreateTweet(Tweet t)
        {
            ContextHelper.Context.Tweet.AddObject(t);
            ContextHelper.Context.SaveChanges();
        }

        public void DeleteTweet(Tweet t)
        {
            Tweet serverTweet = getTweetFromId(t.id).FirstOrDefault();
            if (serverTweet == null) return;
            ContextHelper.Context.Tweet.DeleteObject(serverTweet);
            ContextHelper.Context.SaveChanges();
        }

        public void UpdateTweet(Tweet t)
        {
            ContextHelper.Context.SaveChanges();
        }

        public IEnumerable<Tweet> GetAllTweetsFromTag(string _tag)
        {
            var query = from tweet in ContextHelper.Context.Tweet
                        from message in ContextHelper.Context.Message
                        from tag in ContextHelper.Context.Tag
                        where message.TagProperty.Contains(tag)
                        where message.TweetProperty.Contains(tweet)
                        where tag.nom == _tag
                        where tweet.UserFromProperty == null
                        orderby tweet.tweetdate descending
                        select tweet;

            return query;
        }

        public IEnumerable<Tweet> GetAllTweetsFromTexte(string _texte)
        {
            var query = from tweet in ContextHelper.Context.Tweet
                        from message in ContextHelper.Context.Message
                        where message.TweetProperty.Contains(tweet)
                        where message.contenu.Contains(_texte)
                        where tweet.UserFromProperty == null
                        orderby tweet.tweetdate descending
                        select tweet;

            return query;
        }

        public IEnumerable<Tweet> GetUserTimeline(User user)
        {
            var query = (from tweet in ContextHelper.Context.Tweet
                        from abonnement in ContextHelper.Context.Abonnement
                        where tweet.twitter == abonnement.publisher
                        where abonnement.follower == user.id
                         where (tweet.twitter == user.id) || (tweet.twitter == abonnement.publisher)
                        orderby tweet.tweetdate descending
                        select tweet).ToList();


            // On va supprimer maintenant les tweets qui sont retweeter.

            // Récupération des messages non retweeté par l'utilisateur
            var query2 = from tweet in query
                         where tweet.UserFromProperty != null || 
                            (tweet.UserFromProperty == null && tweet.twitter != user.id)
                         select tweet;

            List<Tweet> tweets = new List<Tweet>(query2);

            // On vérifie et supprime les tweets retwittés par l'utilisateur
            foreach (Tweet tweet in tweets)
            {
                if (HasBeenRetweeted(tweet, query, user))
                    query.Remove(tweet);
            }

            return query;
        }

        private bool HasBeenRetweeted(Tweet tweet, IEnumerable<Tweet> tweets, User user)
        {
            var query = from t in tweets
                        where t.creator == tweet.creator
                        where t.message == tweet.message
                        where t.twitter == user.id
                        where t.id != tweet.id

                        select t;

            return query.Count() != 0;
        }

        public IEnumerable<Tweet> getTweetFromId(long id)
        {
            var query = from tweet in ContextHelper.Context.Tweet
                        where tweet.id == id
                        select tweet;

            return query;
        }

        public IEnumerable<Tweet> GetAllTweetsByPublisher(string texte)
        {
            var query = from tweet in ContextHelper.Context.Tweet
                        from user in ContextHelper.Context.User
                        where tweet.creator == user.id
                        where user.login.Contains(texte)
                        orderby tweet.tweetdate descending
                        select tweet;

            return query;
        }

        public IEnumerable<Tweet> GetAllTweetsByUserId(long id)
        {
            var query = from tweet in ContextHelper.Context.Tweet
                        //where tweet.creator == id || tweet.twitter == id || tweet.userfrom == id
                        where tweet.twitter == id
                        orderby tweet.tweetdate descending
                        select tweet;

            return query;
        }
    }


}
